class FsetsController < ApplicationController
  # GET /fsets
  # GET /fsets.xml
  
  before_filter :check_logged_in, :only => [:destroy, :update, :create, :edit, :new, :fplay]
  
  def index
    if logged_in?  
      @my_fsets = current_user.fsets
	    @my_fsets = @my_fsets.collect {|set| set.name = set.name + " (" + set.flashcards.count.to_s + ")"; set}
      @all_fsets = Fset.find(:all, :conditions => ["user_id != ?", current_user.id])
    else
      @all_fsets = Fset.find(:all)
    end
    @all_fsets = @all_fsets.collect {|set| set.name = set.name + " (" + set.flashcards.count.to_s + ")"; set}

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @fsets }
    end
  end

  # GET /fsets/1
  # GET /fsets/1.xml
  def show
    @fset = Fset.find(params[:id])
    @flashcards = @fset.flashcards
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @fset }
    end
  end

  # GET /fsets/new
  # GET /fsets/new.xml
  def new
    @fset = Fset.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @fset }
    end
  end

  # GET /fsets/1/edit
  def edit
    @fset = Fset.find(params[:id])
    @flashcards = @fset.flashcards
    @new_flashcard = Flashcard.new
    if (@fset.user != current_user) 
      access_denied
    end
  end

  # POST /fsets
  # POST /fsets.xml
  def create
    @fset = Fset.new(params[:fset])
    @fset.user = current_user
#    @flashcards = @fset.flashcards
#    @new_flashcard = Flashcard.new

    respond_to do |format|
      if @fset.save
        flash[:notice] = 'Fset was successfully created.'
        format.html { redirect_to(edit_fset_path(@fset)) }
#        format.html { render :action => "edit" }
        format.xml  { render :xml => @fset, :status => :created, :location => @fset }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @fset.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /fsets/1
  # PUT /fsets/1.xml
  def update
    @fset = Fset.find(params[:id])
    if (@fset.user != current_user) ###this repeats code from edit.  I feel dirty.
      access_denied
    end

    respond_to do |format|
      if @fset.update_attributes(params[:fset])
        flash[:notice] = 'Fset was successfully updated.'
        format.html { redirect_to(edit_fset_path(@fset)) }
#        format.html { render :action => "edit" }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @fset.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /games/1
  # PUT /games/1.xml
  def fplay
    @fset = Fset.find(params[:id])
	session[:game_format] = 0
	respond_to do |format|
        format.html { redirect_to(new_fset_game_path(@fset)) }
    end
  end
  
  # DELETE /fsets/1
  # DELETE /fsets/1.xml
  def destroy
    @fset = Fset.find(params[:id])
    if (@fset.user != current_user) ###this repeats code from edit.  I feel dirty.
      access_denied
    else
      @fset.destroy
    
      respond_to do |format|
        format.html { redirect_to(fsets_url) }
        format.xml  { head :ok }
      end
    end
  end
end
